package com.main;


import com.utils.AdbCmdUtils;
import com.utils.CmdComtans;
import com.utils.OpenCvUtils;

import java.io.File;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Scanner;

/***
 * @Author maochuang.li
 *
 * @Email maochuang.li@fuyinhy.com
 *
 * @Date Create in 15:55 2018/1/4 0004
 */
public class JumpMian {

    private static String adbFilePath;

    private static AdbCmdUtils adbCmdUtils;

    private static String outFilePath = "C:\\Users\\Administrator\\Desktop\\test1\\";

    /**
     * 加载Adb工具
     */
    static {
        URL adb = JumpMian.class.getClassLoader().getResource("adb");
        adbFilePath = adb.getFile().substring(1) + "/adb.exe";
        adbCmdUtils = new AdbCmdUtils(adbFilePath);
    }


    /**
     * 脚本入口
     *
     * @param args
     * @Author maochuang.li
     * @Date Create in 15:56 2018/1/4 0004
     */
    public static void main(String[] args) {
        System.out.println("=====================微信跳跳助手======================");
        System.out.println("==  1、检测设备是否连接                               ==");
        System.out.println("==  2、开始游戏                                      ==");
        System.out.println("==  3、结束游戏                                      ==");
        System.out.println("=====================微信跳跳助手======================");
        System.out.print("输入操作序号：");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String next = scanner.next();
            switch (next) {
                case "1":
                    adbCmdUtils.runExe(CmdComtans.DEVICES);
                    System.out.print("输入操作序号：");
                    break;
                case "2":
                    start();
                    break;
                case "3": {
                    System.exit(0);
                    break;
                }
            }
        }
    }

    /***
     * 开始游戏
     */
    private static void start() {
        while (true) {
            //截取手机图片
            pringtImageDown();
            //识别图片坐标
            File file = new File("jump_jump.png");
            int[][] resultXy = new int[2][2];
            try {
                TestImage.getAllxy(file.getAbsolutePath(), resultXy);
                TestImage.drawPointImage(file.getAbsolutePath(), resultXy, "D:\\");
                resultXy[1] = OpenCvUtils.getStardXy(file);
                TestImage.drawPointImage(file.getAbsolutePath(), resultXy, "D:\\");
                String times = getTimes(resultXy);
                StringBuffer buffer = new StringBuffer(CmdComtans.SWIPE);
                buffer.append(resultXy[0][0]).append(" ").append(resultXy[0][1]).append(" ");
                buffer.append(resultXy[1][0]).append(" ").append(resultXy[1][1]).append(" ");
                buffer.append(times);
                String cmd = buffer.toString();
                System.out.println(cmd);
                //开始滑动
                adbCmdUtils.runExe(cmd);
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 计算毫秒数
     *
     * @param resultXy
     * @return
     */
    private static String getTimes(int[][] resultXy) {
        DecimalFormat df = new DecimalFormat("######0");
        int subX = Math.abs(resultXy[0][0] - resultXy[1][0]);
        int subY = Math.abs(resultXy[0][1] - resultXy[1][1]);
        subX = subX * subX;
        subY = subY * subY;
        double pow = Math.sqrt(subY + subX);
        System.out.println(pow);
        double v = pow * 2.1;
        String format = df.format(v);
        return format;
    }


    /**
     * 截屏并下载展示图片
     *
     * @Author maochuang.li
     * @Date Create in 15:57 2018/1/4 0004
     */
    private static void pringtImageDown() {
        //开始截图
        adbCmdUtils.runExe(CmdComtans.PRINT_SCREENCAP);
        //下载文件
        adbCmdUtils.runExe(CmdComtans.DOWNLOAD);
        //GUI 展示图片并显示
    }


}
